using System;
using System.Web.Mvc;
using WASM.WebSite.DataEntities;
using WASM.WebSite.Models;
using System.Linq;
using WASM.WebSite.Logic.Storage;

namespace WASM.WebSite.Controllers
{
	[Authorize]
	public class AzureStorageController : Controller
	{
		public ActionResult List()
		{
			ViewData.Model = AzureStorageUserSettingsRepository.List();
			return View();
		}

		[AcceptVerbs(HttpVerbs.Get)]
		public ActionResult Create()
		{
			InitAzureStorageType();
			return View();
		}

		[AcceptVerbs(HttpVerbs.Post)]
		public ActionResult Create(AzureStorage azureStorage)
		{
			Validate(azureStorage);
			if (ModelState.IsValid)
			{
				AzureStorageUserSettingsRepository.Insert(azureStorage);
				return RedirectToAction("List");
			}
			else
			{
				InitAzureStorageType();
				return View();
			}
		}

		[AcceptVerbs(HttpVerbs.Get)]
		public ActionResult CreateSql()
		{
			return View();
		}

		[AcceptVerbs(HttpVerbs.Post)]
		public ActionResult CreateSql(AzureStorage azureStorage)
		{
			ValidateSql(azureStorage);
			if (ModelState.IsValid)
			{
				SetSqlAzureStorage(azureStorage);
				AzureStorageUserSettingsRepository.Insert(azureStorage);
				return RedirectToAction("List");
			}
			else
			{
				return View();
			}
		}

		[AcceptVerbs(HttpVerbs.Get)]
		public ActionResult Edit(Guid id)
		{
			ViewData.Model = AzureStorageUserSettingsRepository.Single(id);
			InitAzureStorageType();
			return View();
		}

		[AcceptVerbs(HttpVerbs.Post)]
		public ActionResult Edit(Guid id, AzureStorage azureStorage)
		{
			Validate(azureStorage);
			if (ModelState.IsValid)
			{
				azureStorage.AzureStorageId = id;
				AzureStorageUserSettingsRepository.Update(azureStorage);
				return RedirectToAction("List");
			}
			else
			{
				InitAzureStorageType();
				ViewData.Model = azureStorage;
				return View();
			}
		}

		[AcceptVerbs(HttpVerbs.Get)]
		public ActionResult EditSql(Guid id)
		{
			ViewData.Model = AzureStorageUserSettingsRepository.Single(id);
			return View();
		}

		[AcceptVerbs(HttpVerbs.Post)]
		public ActionResult EditSql(Guid id, AzureStorage azureStorage)
		{
			ValidateSql(azureStorage);
			if (ModelState.IsValid)
			{
				azureStorage.AzureStorageId = id;
				SetSqlAzureStorage(azureStorage);
				AzureStorageUserSettingsRepository.Update(azureStorage);
				return RedirectToAction("List");
			}
			else
			{
				ViewData.Model = azureStorage;
				return View();
			}
		}

		private void Validate(AzureStorage azureStorage)
		{
			if (String.IsNullOrEmpty(azureStorage.asName))
				ModelState.AddModelError("asName", "Name is empty");

			if (String.IsNullOrEmpty(azureStorage.asUrl))
				ModelState.AddModelError("asUrl", "Url is empty");

			if (String.IsNullOrEmpty(azureStorage.asKey))
				ModelState.AddModelError("asKey", "Key is empty");

			if (String.IsNullOrEmpty(azureStorage.asAccountName))
				ModelState.AddModelError("asAccountName", "AccountName is empty");
		}

		private void ValidateSql(AzureStorage azureStorage)
		{
			if (String.IsNullOrEmpty(azureStorage.asName))
				ModelState.AddModelError("asName", "Name is empty");

			if (String.IsNullOrEmpty(azureStorage.asUrl))
				ModelState.AddModelError("asUrl", "Connection sting is empty");
		}

		private void SetSqlAzureStorage(AzureStorage azureStorage)
		{
			azureStorage.asKey = AzureStorageTypeEnum.Sql.ToString();
			azureStorage.asAccountName = AzureStorageTypeEnum.Sql.ToString();
			azureStorage.AzureStorageTypeId = (int)AzureStorageTypeEnum.Sql; 
		}

		public void InitAzureStorageType()
		{
			AzureStorageType[] azureStorageTypesWithoutSql =
				AzureStorageUserSettingsRepository.AzureStorageTypeList().
				Where(e => e.AzureStorageTypeId != (int)AzureStorageTypeEnum.Sql).ToArray();

			ViewData["AzureStorageType"] = new SelectList(azureStorageTypesWithoutSql, "AzureStorageTypeId", "astName");
		}

		public ActionResult Delete(Guid id)
		{
			AzureStorageUserSettingsRepository.Delete(id);
			return RedirectToAction("List");
		}
	}
}